package com.autolinq.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import sun.misc.BASE64Decoder;

import com.autolinq.pojo.WeChatMessage;

public abstract class AbstractWechatService implements DPCAWechatService{
	protected static Properties prop = new Properties();
	
	// 将 s 进行 BASE64 编码
		public static String getBASE64(String s) {
			if (s == null)
				return null;
			return new String(org.apache.commons.codec.binary.Base64.encodeBase64(s.getBytes(),false));
			//return new String(org.apache.commons.codec.binary.Base64.encodeBase64(s.getBytes()));
			//return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
		}

		// 将 BASE64 编码的字符串 s 进行解码
		public static String getFromBASE64(String s) {
			if (s == null)
				return null;
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				byte[] b = decoder.decodeBuffer(s);
				return new String(b);
			} catch (Exception e) {
				return null;
			}
		}
	
	public void handleProperties(Properties properties){
		this.prop = properties;
	}
	
	public String getWechatWelcomePage(WeChatMessage message) {
		String serverURL = prop.getProperty("serverURL");
		String returnStr = "";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		String times = format.format(new Date());

		Element rootXML = new Element("xml");

		rootXML.addContent(new Element("ToUserName").setText(message.getFromUserName()));

		rootXML.addContent(new Element("FromUserName").setText(message.getToUserName()));

		rootXML.addContent(new Element("CreateTime").setText(times));

		rootXML.addContent(new Element("MsgType").setText("news"));

		rootXML.addContent(new Element("ArticleCount").setText("4"));

		Element fXML = new Element("Articles");

		Element mXML = new Element("item");
		mXML.addContent(new Element("Title").setText("结伴出游1.0"));
		mXML.addContent(new Element("Description").setText(""));
		mXML.addContent(new Element("PicUrl").setText(serverURL+"/img/big_pic.png"));
		mXML.addContent(new Element("Url").setText(serverURL+"/trip_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML);
		
		Element mXML2 = new Element("item");
		mXML2.addContent(new Element("Title").setText("旅程"));
		mXML2.addContent(new Element("Description").setText(""));
		mXML2.addContent(new Element("PicUrl").setText(serverURL+"/img/trip.png"));
		mXML2.addContent(new Element("Url").setText(serverURL+"/trip_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML2);
		
		Element mXML3 = new Element("item");
		mXML3.addContent(new Element("Title").setText("搭车"));
		mXML3.addContent(new Element("Description").setText(""));
		mXML3.addContent(new Element("PicUrl").setText(serverURL+"/img/car.png"));
		mXML3.addContent(new Element("Url").setText(serverURL+"/car_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML3);
		
		Element mXML4 = new Element("item");
		mXML4.addContent(new Element("Title").setText("我的"));
		mXML4.addContent(new Element("Description").setText(""));
		mXML4.addContent(new Element("PicUrl").setText(serverURL+"/img/me.png"));
		mXML4.addContent(new Element("Url").setText(serverURL+"/friend_list.html?openid="+(message.getOpenID()==null?message.getFromUserName():"")));
		fXML.addContent(mXML4);

		rootXML.addContent(fXML);

		Document doc = new Document(rootXML);

		XMLOutputter XMLOut = new XMLOutputter();

		returnStr = XMLOut.outputString(doc);

		return returnStr;

	}
	
	public abstract String handleWechatMessage(WeChatMessage message,String qrcodePath);
}